package icu.ikuns.servlet;

import icu.ikuns.pojo.Triangle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author： Kimariyb
 * @Date：2022/05/22/15:10
 * @Description：
 */
public class CalServlet extends HttpServlet {
    public CalServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String edgeA = req.getParameter("edgeA");
        String edgeB = req.getParameter("edgeB");
        String edgeC = req.getParameter("edgeC");
        if (edgeA == null) {
            edgeA = "0";
        }        if (edgeB == null) {
            edgeA = "0";
        }        if (edgeC == null) {
            edgeA = "0";
        }
        double d1 = 0, d2 = 0, d3 = 0;
        try {
            d1 = Double.parseDouble(edgeA);
            d2 = Double.parseDouble(edgeB);
            d3 = Double.parseDouble(edgeC);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        Triangle triangle = new Triangle(d1, d2, d3);
        HttpSession session = req.getSession();
        if (!triangle.isTriangle()) {
            session.setAttribute("message", "不构成三角形");
        } else {
            session.setAttribute("message", "面积为" + triangle.getArea());
        }
        resp.sendRedirect("inputTriangle.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
